﻿using Beryl.Util;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls.WebParts;

namespace Beryl.Model
{
    public class CuttingRev
    {
        private static CuttingRev instance = null;
        private static readonly object uniqueLock = new object();

        public static CuttingRev Instance
        {
            get
            {
                lock (uniqueLock)
                {
                    if (instance == null)
                    {
                        instance = new CuttingRev();
                    }
                }
                return instance;
            }
        }

        public CuttingRev() { }

        
        public List<string> getCQReV(string data,string Data)
        {
            List<string> list = new List<string>();
            string name = System.Reflection.MethodBase.GetCurrentMethod().Name;
            Newtonsoft.Json.Linq.JArray jsonArr = (Newtonsoft.Json.Linq.JArray)JsonConvert.DeserializeObject(data);//解析为数组
            var k = jsonArr[0];
            try
            {

                string strPicYZ1 = "";
                string sql = string.Format(@"select * from View_CutReceive where cOrderNo = '{0}' order by dPlanQty desc,dWidth desc", k["barcode"].ToString().Split('-')[1]);
                strPicYZ1 = JsonConvert.SerializeObject(CommonMethod.Instance.selectDatabase(sql));
                if (strPicYZ1.Equals("[]"))
                {
                    list.Add("此流传单号错误");
                }
                else
                {
                    list.Add(strPicYZ1);
                }
            }
            catch (Exception e)
            {
                list.Add(e.Message);
                //CommonMethod.Instance.InsertLog(name, e.Message, CommonMethod.LogType.Error, Data, "裁切接收获取流传单错误");
                CommonMethod.Instance.InsertLog1(k["barcode"].ToString().Split('-')[1], e.Message, Data, CommonMethod.LogType.Error, name, "", true);
                return list;
            }
            //CommonMethod.Instance.InsertLog(name, list[0], CommonMethod.LogType.Info, Data, "裁切接收获取流传单信息");
            CommonMethod.Instance.InsertLog1(k["barcode"].ToString().Split('-')[1], list[0], Data, CommonMethod.LogType.Info, name, "", true);
            return list;
        }

        //裁切接收提交
        public string summitCQReV(string para, string Data)
        {
            string name = System.Reflection.MethodBase.GetCurrentMethod().Name;
            JArray jsonArr = (JArray)JsonConvert.DeserializeObject(para);
            JArray jsonArr1 = (JArray)JsonConvert.DeserializeObject(jsonArr[jsonArr.Count - 1].ToString());
            string updateSql = "";
            string sql4 = "";
            string insertSql1 = "INSERT INTO WMS_Receive(cMatNo, Category, createTime, cMOLotNo, cBatch, fQuantity, cSpecifucation, dProductDate, dInputDate, cProductDepNo, warehousecode, cSupplier, inum, irate, cPlace) VALUES";
            string insertSql2 = "INSERT INTO WMS_Retrieval(cMatNo, Category, createTime, cOrderNo, cMOLotNo, fQuantity, dProductDate, dOutputDate, warehousecode, cSupplier, cProductDepNo, inum, irate) VALUES";
            try
            {
                for (int i = 0; i < jsonArr1.Count; i++) //jsonArr1长度与list长度一致
                {
                    var resData = jsonArr[i];
                    var resData1 = jsonArr1[i];
                    string cMatNo = resData["物料编码"].ToString(); //已切料编码
                    string cMatNo1 = resData1["cMatNo1"].ToString(); //未切料编码
                    double dLength = double.Parse(resData["长度(mm)"].ToString()); //长度
                    double dWidth = double.Parse(resData["宽度(mm)"].ToString()); //宽度
                    double rkWeight = 0.00;
                    if (resData["重量(kg)"].ToString() != "")
                    {
                        rkWeight = double.Parse(resData["重量(kg)"].ToString()); //入库重量
                    }
                    double dPlanQty = double.Parse(resData["片数"].ToString()); //片数
                    double ddz = double.Parse(resData["单重"].ToString()); //单重
                    double dReqQty = double.Parse(resData1["dReqQty"].ToString()); //计划数
                    //var cateNo = cMatNo.substring(0, 5); //已切
                    var cateNo = resData["物料大类"].ToString();
                    var cateNo1 = cMatNo1.Substring(0, 5); //未切

                    double fQuantity = 0; //已切主计量
                    double inum = 0; //已切副计量
                    double irate = 0; //已切换算率
                    double fQuantity1 = 0; //未切主计量
                    double inum1 = 0; //未切副计量
                    double irate1 = 0; //未切换算率

                    if (cateNo == "Y0104" || cateNo == "Y0192" || cateNo == "Y0105")
                    { //已切正负箔
                        fQuantity = Math.Round(ddz * rkWeight / dWidth * 1000, 2); //单位米数
                        inum = Math.Round(rkWeight, 2); //单位kg
                        irate = Math.Round(1 / ddz, 6);
                    }

                    if (cateNo == "Y0106") //已切电解纸
                    {
                        fQuantity = Math.Round(rkWeight, 2); //单位kg
                        inum = Math.Round(dWidth * dPlanQty * dLength / 1000, 2); //单位平方米
                    }

                    if (cateNo1 == "Y0101" || cateNo1 == "Y0191" || cateNo1 == "Y0102")
                    { //未切正负箔
                        fQuantity1 = Math.Round(ddz * rkWeight, 2); //单位平方米
                        inum1 = Math.Round(rkWeight, 2); //单位kg
                        irate1 = Math.Round(1 / ddz, 6);
                    }

                    if (cateNo1 == "Y0103") //未切电解纸
                    {
                        fQuantity1 = Math.Round(rkWeight, 2); //单位kg
                        inum1 = Math.Round(ddz * rkWeight, 2); //单位平方米
                    }

                    string cBatchNumber = resData["出厂编号"].ToString() == null ? "" : resData["出厂编号"].ToString(); //未切料批次号
                    string cMOLotNo = resData1["cMOLotNo"].ToString(); //生产批次号
                    string cOrderNo = resData1["cOrderNo"].ToString(); ; //生产订单号
                    string cSpecifution = resData["规格型号"].ToString() == null ? "" : resData["规格型号"].ToString(); //已切料规格
                    string dProductDate = resData1["dProductDate"].ToString(); //已切料生产日期
                    string times = DateTime.Now.ToString();
                    string dInputDate = times;
                    string dOutputDate = times;
                    string cSupplierInfo = resData1["cSupplierInfo"].ToString() == null ? "" : resData1["cSupplierInfo"].ToString(); //未切料供应商名称
                    string cPlace = resData["库位"].ToString() == null ? "" : resData["库位"].ToString(); //入库货位
                    string cProductDepNo = resData1["cProductDepNo"].ToString();
                    string warehousecode = resData1["warehousecode"].ToString(); //wh; //入库仓库

                    string warehousecode1 = "21-1"; //出库仓库

                    if (i < jsonArr.Count - 1)
                    {
                        //已切料写入入库台账WMS_Receive
                        insertSql1 += "('" + cMatNo + "'," + 3 + ",'" + times +
                            "','" + cMOLotNo + "','" + cOrderNo + "','" + fQuantity + "','" + cSpecifution +
                            "','" + dProductDate + "','" + dInputDate + "','" + cProductDepNo + "','" + warehousecode + "','" + cSupplierInfo + "','" + inum + "'," + irate + ",'" + cPlace + "'),";

                        insertSql2 += "('" + cMatNo1 + "'," + 3 + ",'" + times +
                            "','" + cMOLotNo + "','" + cBatchNumber + "','" + fQuantity1 + "','" + dProductDate +
                            "','" + dOutputDate + "','" + warehousecode1 + "','" + cSupplierInfo + "','" + cProductDepNo + "','" + inum1 + "'," + irate1 + "),";
                    }
                    else
                    {
                        //已切料写入入库台账WMS_Receive
                        insertSql1 += "('" + cMatNo + "'," + 3 + ",'" + times +
                           "','" + cMOLotNo + "','" + cOrderNo + "','" + fQuantity + "','" + cSpecifution +
                           "','" + dProductDate + "','" + dInputDate + "','" + cProductDepNo + "','" + warehousecode + "','" + cSupplierInfo + "','" + inum + "'," + irate + ",'" + cPlace + "');";

                        insertSql2 += "('" + cMatNo1 + "'," + 3 + ",'" + times +
                            "','" + cMOLotNo + "','" + cBatchNumber + "','" + fQuantity1 + "','" + dProductDate +
                            "','" + dOutputDate + "','" + warehousecode1 + "','" + cSupplierInfo + "','" + cProductDepNo + "','" + inum1 + "'," + irate1 + ");";
                    }
                    updateSql += string.Format(@"update APS_ValidBOM set cCutFlag = '0' where cOrderType = '3' and cOrderNo = '{0}' and cSpecifution = '{1}' and cBatchNumber = '{2}';", cOrderNo, cSpecifution, cBatchNumber);
                }
                sql4 = insertSql1 + insertSql2 + updateSql;
                //执行操作
                SqlHelper.IniConnString();
                var connstr = SqlHelper.GetConnSting();
                SqlHelper.ExecuteNonQuery(connstr, CommandType.Text, sql4);

            }
            catch (Exception e)
            {
                //CommonMethod.Instance.InsertLog(name, e.Message, CommonMethod.LogType.Error, Data, "裁切接收提交失败");
                CommonMethod.Instance.InsertLog1("", e.Message, Data, CommonMethod.LogType.Error, name, "", true);
                return e.Message;
            }
            //CommonMethod.Instance.InsertLog(name, "[OK]", CommonMethod.LogType.Info, Data, "裁切接收提交成功");
            CommonMethod.Instance.InsertLog1("", sql4, Data, CommonMethod.LogType.Info, name, "", true);
            return "[OK]";
        }
    }
}